package com.wing.cabinet.controller.admin;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wing.cabinet.model.entity.AboveTheQuotaApply;
import com.wing.cabinet.model.response.AboveTheQuotaApplyVO;
import com.wing.cabinet.service.AboveTheQuotaApplyService;
import com.wing.common.utils.JsonResult;
import com.wing.common.utils.PageResult;
import com.wing.common.utils.RSAUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * 超额申请表管理
 */
@Validated
@Api(value = "Admin-超额申请表管理", tags = "#/cabinet/aboveTheQuotaApply")
@RestController("aboveTheQuotaApplyController")
@RequestMapping("cabinet/admin/aboveTheQuotaApply")
@RequiredArgsConstructor
@Slf4j
public class AboveTheQuotaApplyController {

    private final AboveTheQuotaApplyService aboveTheQuotaApplyService;


    @ApiOperation(value = "查询所有超额申请表", notes = "搜索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "第几页", defaultValue = "1", example = "1", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "limit", value = "每页多少条", defaultValue = "10", example = "10", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "sort", value="排序字段名", defaultValue = "id", example = "id", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "order", value="排序规则(asc:正序, desc:倒序)", defaultValue = "asc", example = "asc", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "code", value="领新申请编码", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "inventoryOutCode", value="领新记录编号", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "creatorName", value="申请人名称", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "authorityManageName", value="权限组名称", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "mainCabCode", value="柜子编号", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "checkStatus", value="审核状态:(0,待初审);(1,初审驳回);(2,初审通过);(3,待复审);(4,复审驳回);(5,复审通过）", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "startCreateTime", value="开始时间", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "endCreateTime", value="结束时间", dataType = "String", paramType = "query")


    })
    @GetMapping("/list")
    public PageResult<AboveTheQuotaApplyVO> list(
            @RequestParam(defaultValue = "1") Integer page
            ,@RequestParam(defaultValue = "10") Integer limit
            ,@RequestParam(defaultValue = "id") String sort
            ,@RequestParam(defaultValue = "desc") String order
            ,String code
            ,String inventoryOutCode
            ,String creatorName
            ,String authorityManageName
            ,String mainCabCode
            ,String checkStatus
            ,String startCreateTime
            ,String endCreateTime
    ){

        Page<AboveTheQuotaApply> pg = new Page<>(page, limit);
        Map<String, Object> params = new HashMap<>();

        //排序
        if (StringUtils.isNotBlank(sort) && StringUtils.isNotBlank(order)) {
            params.put("sort", sort);
            params.put("order", order);
        }
        /*if(StringUtils.isNotBlank(cabinetId)){
            params.put("cabinetId", cabinetId);
        }*/

        params.put("code", code);
        params.put("creatorName", creatorName);
        params.put("inventoryOutCode", inventoryOutCode);
        params.put("authorityManageName", authorityManageName);
        params.put("mainCabCode", mainCabCode);
        params.put("checkStatus", checkStatus);
        params.put("startTime", startCreateTime);
        params.put("endTime", endCreateTime);

        IPage<AboveTheQuotaApplyVO> list = aboveTheQuotaApplyService.pageList4app(pg, params);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }

    @ApiOperation(value = "初审", notes = "初审")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value="超额申请ID", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "checkStatus", value="审核状态: reject(1,初审驳回);toaudit(2,初审通过)", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "remark", value="备注", dataType = "String", paramType = "query")


    })
    @GetMapping("/firstCheck")
    public JsonResult firstCheck(ServletRequest servletRequest, Long id, AboveTheQuotaApply.CheckStatus checkStatus, String remark, String items) {
        try {
            HttpServletRequest request = (HttpServletRequest)servletRequest;
            String domain = request.getHeader("domain");
            aboveTheQuotaApplyService.firstCheck(id, checkStatus, remark, domain, items);
        }catch (Exception e){
            log.error("初审失败！",e);
            return JsonResult.fail("初审失败！"+e.getMessage());
        }
        return JsonResult.success("初审成功！");
    }

    @ApiOperation(value = "复审", notes = "复审")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value="超额申请ID", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "checkStatus", value="审核状态: rCheckReject(4,复审驳回);rCheckAudit(5,复审通过）", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "remark", value="备注", dataType = "String", paramType = "query")


    })
    @GetMapping("/againCheck")
    public JsonResult againCheck(Long id, AboveTheQuotaApply.CheckStatus checkStatus, String remark, String items) {
        try {
            aboveTheQuotaApplyService.againCheck(id, checkStatus, remark, items);
        }catch (Exception e){
            log.error("复审失败！",e);
            return JsonResult.fail("复审失败！"+e.getMessage());
        }
        return JsonResult.success("复审成功！");
    }


    @GetMapping("/firstCheckAudit")
    public String firstCheckAudit(ServletRequest servletRequest, String signData) {
        try {
            HttpServletRequest request = (HttpServletRequest)servletRequest;
            String domain = request.getHeader("domain");
            if (StringUtils.isBlank(signData)){
                return "缺少签名数据";
            }
            signData=signData.replaceAll(" ","+");
            String id="";
            try{
                id= RSAUtils.decryptByPrivateKey(RSAUtils.privateKey,signData);
            }catch (Exception e){
                log.error("audit fail",e);
                return "签名数据错误,审核失败！";
            }
            aboveTheQuotaApplyService.firstCheck(Long.parseLong(id), AboveTheQuotaApply.CheckStatus.toaudit, null, domain,null);
        }catch (Exception e){
            log.error("初审失败！",e);
            return "初审失败！"+e.getMessage();
        }
        return "初审成功！";
    }

    @GetMapping("/firstCheckReject")
    public String firstCheckReject(ServletRequest servletRequest, String signData) {
        try {
            HttpServletRequest request = (HttpServletRequest)servletRequest;
            String domain = request.getHeader("domain");
            if (StringUtils.isBlank(signData)){
                return "缺少签名数据";
            }
            signData=signData.replaceAll(" ","+");
            String id="";
            try{
                id= RSAUtils.decryptByPrivateKey(RSAUtils.privateKey,signData);
            }catch (Exception e){
                log.error("audit fail",e);
                return "签名数据错误,审核失败！";
            }
            aboveTheQuotaApplyService.firstCheck(Long.parseLong(id), AboveTheQuotaApply.CheckStatus.reject, null, domain,null);
        }catch (Exception e){
            log.error("初审失败！",e);
            return "初审失败！"+e.getMessage();
        }
        return "初审成功！";
    }

    @GetMapping("/againCheckAudit")
    public String againCheckAudit(String signData) {
        try {
            if (StringUtils.isBlank(signData)){
                return "缺少签名数据";
            }
            signData=signData.replaceAll(" ","+");
            String id="";
            try{
                id= RSAUtils.decryptByPrivateKey(RSAUtils.privateKey,signData);
            }catch (Exception e){
                log.error("audit fail",e);
                return "签名数据错误,审核失败！";
            }
            aboveTheQuotaApplyService.againCheck(Long.parseLong(id), AboveTheQuotaApply.CheckStatus.rCheckAudit, null,null);
        }catch (Exception e){
            log.error("复审失败！",e);
            return "复审失败！"+e.getMessage();
        }
        return "复审成功！";
    }

    @GetMapping("/againCheckReject")
    public String againCheckReject(String signData) {
        try {
            if (StringUtils.isBlank(signData)){
                return "缺少签名数据";
            }
            signData=signData.replaceAll(" ","+");
            String id="";
            try{
                id= RSAUtils.decryptByPrivateKey(RSAUtils.privateKey,signData);
            }catch (Exception e){
                log.error("audit fail",e);
                return "签名数据错误,审核失败！";
            }
            aboveTheQuotaApplyService.againCheck(Long.parseLong(id), AboveTheQuotaApply.CheckStatus.rCheckReject, null,null);
        }catch (Exception e){
            log.error("复审失败！",e);
            return "复审失败！"+e.getMessage();
        }
        return "复审成功！";
    }
}
